package com.example.java_gobang.game;


import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserManager {
    //通过这个哈希表来表示当前用户在 游戏大厅 在线状态
//    private HashMap<Integer, WebSocketSession> gameHall = new HashMap<>();
    //使用hashMap会有线程安全问题
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();

    //这个哈希表用来表示当前用户再 游戏房间 的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    public void enterGameHall(int userId, WebSocketSession webSocketSession){
        gameHall.put(userId, webSocketSession);
    }

    public void exitGameHall(int userId){
        gameHall.remove(userId);
    }

    public WebSocketSession getFromGameHall(int userId){
        return gameHall.get(userId);
    }


    public void enterGameRoom(int userId, WebSocketSession webSocketSession){
        gameRoom.put(userId, webSocketSession);
    }

    public void exitGameRoom(int userId){
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromGameRoom(int userId){
        return gameRoom.get(userId);
    }
}
